package com.hua.yuan.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.google.gson.Gson;
import com.hua.yuan.BaseFragment;
import com.hua.yuan.R;
import com.hua.yuan.base.Constents;
import com.hua.yuan.base.ITabFragment;
import com.hua.yuan.ui.ActivityBoYueJinXuan;
import com.hua.yuan.ui.ActivityFuFJp;
import com.hua.yuan.ui.ActivityHotComment;
import com.hua.yuan.ui.ActivityTeJia;
import com.hua.yuan.ui.data.VideoDataRequest;
import com.hua.yuan.ui.module.MovieInfo69Bean;
import com.hua.yuan.ui.module.MovieRotBean;
import com.hua.yuan.util.ToastUtil;
import com.hua.yuan.view.MyBannerView;
import com.hua.yuan.view.VideoItemView;
import com.hua.yuan.view.VideoItemViewFor;
import com.hua.yuan.view.VideoItemViewThree;
import com.hua.yuan.view.VideoItemViewTwo;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cm.hua.yuan.common.utils.AppContextManager;
import cm.hua.yuan.common.utils.PreferenceUtils;
import okhttp3.Call;

/**
 * Created by huangyang on 2018/5/8.
 * 视频的fragment
 */

public class VideoFragment extends BaseFragment implements View.OnClickListener,ITabFragment {

    private RelativeLayout videoSearchRl;

    private MyBannerView myBannerView;

    private LinearLayout videoTwoLl1;

    private LinearLayout videoTwoLl2;

    private LinearLayout videoTwoLl3;

    private LinearLayout videoTwoLl4;

    private VideoItemView itemViewHot;
    private VideoItemViewTwo itemViewPay;
    private VideoItemViewThree itemViewMonth;
    private VideoItemViewFor itemViewTj;


    private List<MovieInfo69Bean> hotList;
    private List<MovieInfo69Bean> payList;
    private List<MovieInfo69Bean> monthList;
    private List<MovieInfo69Bean> tJList;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_video, null);
        initView(view);

        PreferenceUtils.setInt(Constents.HOT_DATA_SATRT, 0);
        PreferenceUtils.setInt(Constents.FU_FEI_DATA_START, 0);
        PreferenceUtils.setInt(Constents.BAO_YUE_DATA_START, 0);
        PreferenceUtils.setInt(Constents.TEI_JIA_DATA_START, 0);


        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        hotList = new ArrayList<>();
        payList = new ArrayList<>();
        monthList = new ArrayList<>();
        tJList = new ArrayList<>();

        initData();

    }

    private void initView(View view) {

        videoSearchRl = view.findViewById(R.id.videoSearchRl);
        myBannerView = view.findViewById(R.id.myBannerView);

        videoTwoLl1 = view.findViewById(R.id.videoTwoLl1);
        videoTwoLl2 = view.findViewById(R.id.videoTwoLl2);
        videoTwoLl3 = view.findViewById(R.id.videoTwoLl3);
        videoTwoLl4 = view.findViewById(R.id.videoTwoLl4);

        itemViewHot = view.findViewById(R.id.itemViewHot);
        itemViewPay = view.findViewById(R.id.itemViewPay);
        itemViewMonth = view.findViewById(R.id.itemViewMonth);
        itemViewTj = view.findViewById(R.id.itemViewTj);

    }


    private void initData() {

        videoSearchRl.setOnClickListener(this);
        videoTwoLl1.setOnClickListener(this);
        videoTwoLl2.setOnClickListener(this);
        videoTwoLl3.setOnClickListener(this);
        videoTwoLl4.setOnClickListener(this);

        createData();

    }

    private void createData() {

        String token = PreferenceUtils.getString(Constents.TOKEN, "");

        VideoDataRequest.getMovieIndex(token, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

                e.printStackTrace();

            }

            @Override
            public void onResponse(String response, int id) {


                try {
                    JSONObject jsonObject = new JSONObject(response);

                    if (!jsonObject.isNull("errno")) {
                        String code = jsonObject.getString("errno");
                        if ("0".equals(code)) {

                            if (!jsonObject.isNull("data")) {
                                JSONObject dataJson = jsonObject.getJSONObject("data");

                                if (!dataJson.isNull("hot")) {
                                    //热门

                                    JSONArray hotJson = dataJson.getJSONArray("hot");

                                    Gson gson = new Gson();

                                    if (hotList == null) {
                                        hotList = new ArrayList<>();
                                    }

                                    for (int i = 0; i < hotJson.length(); i++) {
                                        JSONObject hot = hotJson.getJSONObject(i);
                                        MovieInfo69Bean bean = gson.fromJson(hot.toString(), MovieInfo69Bean.class);
                                        if (bean != null) {
                                            hotList.add(bean);
                                        }
                                    }

//

                                    if (itemViewHot != null) {
                                        itemViewHot.setData(hotList);
                                    }

                                }

                                if (!dataJson.isNull("pay")) {
                                    //付费

                                    JSONArray payJson = dataJson.getJSONArray("pay");

                                    Gson gson = new Gson();

                                    if (payList == null) {
                                        payList = new ArrayList<>();
                                    }

                                    for (int i = 0; i < payJson.length(); i++) {
                                        JSONObject hot = payJson.getJSONObject(i);
                                        MovieInfo69Bean bean = gson.fromJson(hot.toString(), MovieInfo69Bean.class);
                                        if (bean != null) {
                                            payList.add(bean);
                                        }
                                    }

                                    if (itemViewPay != null) {
                                        itemViewPay.setData(payList);
                                    }

                                }
                                if (!dataJson.isNull("month")) {
                                    //包月

                                    JSONArray monthJson = dataJson.getJSONArray("month");

                                    Gson gson = new Gson();

                                    if (monthList == null) {
                                        monthList = new ArrayList<>();
                                    }

                                    for (int i = 0; i < monthJson.length(); i++) {
                                        JSONObject month = monthJson.getJSONObject(i);
                                        MovieInfo69Bean bean = gson.fromJson(month.toString(), MovieInfo69Bean.class);
                                        if (bean != null) {
                                            monthList.add(bean);
                                        }
                                    }

                                    if (itemViewMonth != null) {
                                        itemViewMonth.setData(monthList);
                                    }


                                }
                                if (!dataJson.isNull("special")) {
                                    //特价
                                    JSONArray tJJson = dataJson.getJSONArray("special");
                                    Gson gson = new Gson();
                                    if (tJList == null) {
                                        tJList = new ArrayList<>();
                                    }
                                    for (int i = 0; i < tJJson.length(); i++) {
                                        JSONObject tj = tJJson.getJSONObject(i);
                                        MovieInfo69Bean bean = gson.fromJson(tj.toString(), MovieInfo69Bean.class);
                                        if (bean != null) {
                                            tJList.add(bean);
                                        }
                                    }

                                    if (itemViewTj != null) {
                                        itemViewTj.setData(tJList);
                                    }

                                }

                            }


                        }

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });


        VideoDataRequest.getMovieRot(token, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {



                try {
                    JSONObject jsonObject = new JSONObject(response);

                    if (!jsonObject.isNull("errno")) {
                        String code = jsonObject.getString("errno");
                        if ("0".equals(code)) {

                            if (!jsonObject.isNull("data")) {


                                JSONArray jsonArray = jsonObject.getJSONArray("data");

                                Gson gson = new Gson();
                                List<MovieRotBean> list = new ArrayList<>();

                                for (int i = 0; i < jsonArray.length(); i++) {

                                    JSONObject object = jsonArray.getJSONObject(i);
                                    MovieRotBean bean = gson.fromJson(object.toString(), MovieRotBean.class);
                                    list.add(bean);

                                }

                                if (myBannerView != null) {
                                    myBannerView.setData(list);
                                }

                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.videoSearchRl:
                ToastUtil.showShortToast("开发中，敬请期待!");
                break;
            case R.id.videoTwoLl1:
                startActivity(new Intent(context, ActivityHotComment.class));
                break;
            case R.id.videoTwoLl2:
                startActivity(new Intent(context, ActivityFuFJp.class));
                break;
            case R.id.videoTwoLl3:
                startActivity(new Intent(context, ActivityBoYueJinXuan.class));
                break;
            case R.id.videoTwoLl4:
                startActivity(new Intent(context, ActivityTeJia.class));
                break;
        }

    }

    @Override
    public String getTabName() {
        return AppContextManager.getInstance().getContext().getString(R.string.video);
    }

    @Override
    public int getDrawableId() {
        return R.mipmap.main_video_unselect;
    }

    @Override
    public int getDrawableSelectId() {
        return R.mipmap.main_video_select;
    }

    @Override
    public Fragment getTabFragment() {
        return this;
    }
}
